package com.dzh.controller;

import com.dzh.pojo.Bill;
import com.dzh.pojo.BillRes;
import com.dzh.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Autowired
    @Qualifier("BillServiceImpl")
    private BillService billService;

    /**
     * 查找所有书籍
     * @param model
     * @return
     */
    @RequestMapping("/allBill")
    public String list(Model model) {
        List<BillRes> list = billService.queryAllBill();
        model.addAttribute("list", list);
        return "allBill";
    }
    /**
     * 去添加书籍
     * @return
     */
    @RequestMapping("/toAddBill")
    public String toAddPaper() {

        return "addBill";
    }

    /**
     * 添加书籍
     * @param bill
     * @return
     */
    @RequestMapping("/addBill")
    public String addPaper(Bill bill) {
        System.out.println(bill);
        billService.addBill(bill);
        return "redirect:/bill/allBill";
    }

    /**
     * 去更新书籍
     * @param model
     * @param billId
     * @return
     */
    @RequestMapping("/toUpdateBill")
    public String toUpdateBill(Model model,@RequestParam("billId") int billId) {
        BillRes bill = billService.queryBillById(billId);
        model.addAttribute("bill",bill );
        return "updateBill";
    }

    /**
     * 更新书籍
     * @param model
     * @param bill
     * @return
     */
    @RequestMapping("/updateBill")
    public String updateBill(Model model, Bill bill) {
        System.out.println(bill);
        try{
            billService.updateBill(bill);
        }catch (Exception e){
            e.printStackTrace();
        }
        BillRes bill1 = billService.queryBillById(bill.getBillId());
        model.addAttribute("bill", bill1);
        return "redirect:/bill/allBill";
    }

    /**
     * 删除书籍
     * @param billId
     * @return
     */
    @RequestMapping("/del/{BillId}")
    public String deleteBill(@PathVariable("billId") int billId) {
        billService.deleteBillById(billId);
        return "redirect:/bill/allBill";
    }




}